package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.annotaion.SystemControllerLog;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年04月24日  11:37
 * 权限控制
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference//查找服务
    private PermissionService permissionService;

    //新增检查项
    @RequestMapping("/add")
    @SystemControllerLog("新增权限失败")
    public Result add(@RequestBody Permission permission) {
        permissionService.add(permission);
        return new Result(true, "新增权限成功");
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除检查项
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")//权限校验
    @RequestMapping("/delete")
    @SystemControllerLog("删除权限失败")
    public Result delete(Integer id) {
        permissionService.deleteById(id);
        return new Result(true, "删除权限成功");
    }

    //编辑检查项
    @RequestMapping("/edit")
    @SystemControllerLog("编辑权限失败")
    public Result edit(@RequestBody Permission permission) {
        permissionService.edit(permission);
        return new Result(true, "编辑权限成功");
    }

    @RequestMapping("/findById")
    @SystemControllerLog("查询权限失败")
    public Result findById(Integer id) {
        Permission permission = permissionService.findById(id);
        return new Result(true, "查询权限成功", permission);
    }

    @RequestMapping("/findAll")
    @SystemControllerLog("查询权限失败")
    public Result findAll() {
        List<Permission> list = permissionService.findAll();
        return new Result(true, "查询权限成功", list);
    }
}
